﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class Vechicle : DBFObject, IDBObject, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        String m_VIN = "";
        public string VIN
        {
            get
            {
                return m_VIN;
            }
            set
            {
                m_VIN = value;
                OnPropertyChanged(new PropertyChangedEventArgs("VIN"));
            }
        }

        String m_DOP_VIN = "";
        public string DOP_VIN
        {
            get
            {
                return m_DOP_VIN;
            }
            set
            {
                m_DOP_VIN = value;
                OnPropertyChanged(new PropertyChangedEventArgs("DOP_VIN"));
            }
        }

        String m_ModelVehicle = "";
        public string ModelVehicle
        {
            get
            {
                return m_ModelVehicle;
            }
            set
            {
                m_ModelVehicle = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ModelVehicle"));
            }
        }

        String m_TypeVehicle = "";
        public string TypeVehicle
        {
            get
            {
                return m_TypeVehicle;
            }
            set
            {
                m_TypeVehicle = value;
                OnPropertyChanged(new PropertyChangedEventArgs("TypeVehicle"));
            }
        }

        String m_Category = "";
        public string Category
        {
            get
            {
                return m_Category;
            }
            set
            {
                m_Category = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Category"));
            }
        }

        DateTime? m_YearManufacture = null;
        public DateTime? YearManufacture
        {
            get
            {
                return m_YearManufacture;
            }
            set
            {
                m_YearManufacture = value;
                OnPropertyChanged(new PropertyChangedEventArgs("YearManufacture"));
            }
        }

        /// <summary>
        /// Страна или организация изготовитель
        /// </summary>
        String m_CountryOrign = "";
        public string CountryOrign
        {
            get
            {
                return m_CountryOrign;
            }
            set
            {
                m_CountryOrign = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CountryOrign"));
            }
        }

        //=====================================================================
        private int m_oRegistrInformation;

        public RegistrInformation ORegistrInformation
        {
            get;
            set;
        }
        private int m_oTechnicalCharacteristics;

        public TechnicalCharacteristics OTechnicalCharacteristics
        {
            get;
            set;
        }

        public Vechicle()
        {
            NameTable = "VEHICLE_TABLE";
            ORegistrInformation = new RegistrInformation();
            OTechnicalCharacteristics = new TechnicalCharacteristics();

            VIN = "";
            DOP_VIN = "";
            ModelVehicle = "";
            TypeVehicle  = "";
            Category = "";
            YearManufacture = null;
            CountryOrign = "";
        }

        object IDBObject.Save()
        {
            try
            {
                Parameters = new Dictionary<string, object>();

                Parameters.Add("ID_REGISTR_INFORMATION", (ORegistrInformation as IDBObject).Save());
                Parameters.Add("ID_TECHNICAL_CHARACTERISTICS", (OTechnicalCharacteristics as IDBObject).Save());


                Parameters.Add("VIN", VIN); 
                Parameters.Add("DOP_VIN", DOP_VIN);
                Parameters.Add("MODEL_VECHICLE", ModelVehicle);
                Parameters.Add("TYPE_VECHICLE", TypeVehicle);
                Parameters.Add("CATEGORY", Category);
                Parameters.Add("YEAR_MANUFACTURE", YearManufacture);
                Parameters.Add("COUNTRY_ORIGN", CountryOrign);

                return SaveObject();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {

                Parameters = new Dictionary<string, object>();
                Parameters.Add("ID_REGISTR_INFORMATION", m_oRegistrInformation);
                Parameters.Add("ID_TECHNICAL_CHARACTERISTICS", m_oTechnicalCharacteristics);

                Parameters.Add("VIN", VIN);
                Parameters.Add("DOP_VIN", DOP_VIN);
                Parameters.Add("MODEL_VECHICLE", ModelVehicle);
                Parameters.Add("TYPE_VECHICLE", TypeVehicle);
                Parameters.Add("CATEGORY", Category);
                Parameters.Add("YEAR_MANUFACTURE", YearManufacture);
                Parameters.Add("COUNTRY_ORIGN", CountryOrign);

                LoadObject(ID);

                ((ORegistrInformation = new RegistrInformation()) as IDBObject).Load(Parameters["ID_REGISTR_INFORMATION"]);
                ((OTechnicalCharacteristics = new TechnicalCharacteristics()) as IDBObject).Load(Parameters["ID_TECHNICAL_CHARACTERISTICS"]);




                VIN = (string)Parameters["VIN"].ToString();
                DOP_VIN = (string)Parameters["DOP_VIN"].ToString();
                ModelVehicle = (string)Parameters["MODEL_VECHICLE"].ToString();
                TypeVehicle = (string)Parameters["TYPE_VECHICLE"].ToString();
                Category = (string)Parameters["CATEGORY"].ToString();
                YearManufacture = Parameters["YEAR_MANUFACTURE"] == null || Parameters["YEAR_MANUFACTURE"].ToString() == String.Empty ? (DateTime?)null : DateTime.Parse(Parameters["YEAR_MANUFACTURE"].ToString());
                CountryOrign = (string)Parameters["COUNTRY_ORIGN"].ToString();
            }
            catch (SystemException err)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }


        void IDBObject.Update()
        {
            (ORegistrInformation as IDBObject).Update();
            (OTechnicalCharacteristics as IDBObject).Update();

            Parameters = new Dictionary<string, object>();
            Parameters.Add("VIN", VIN);
            Parameters.Add("DOP_VIN", DOP_VIN);
            Parameters.Add("MODEL_VECHICLE", ModelVehicle);
            Parameters.Add("TYPE_VECHICLE", TypeVehicle);
            Parameters.Add("CATEGORY", Category);
            Parameters.Add("YEAR_MANUFACTURE", YearManufacture);
            Parameters.Add("COUNTRY_ORIGN", CountryOrign);

            UpdateObject();
        }
    }
}
